//
//  OrderBillViewController.m
//  DaleTou
//
//  Created by 李春阳 on 2018/5/31.
//  Copyright © 2018年 geren. All rights reserved.
//

#import "OrderBillViewController.h"
#import "OrderBillTableViewCell.h"
#import "LotteryOrderView.h"
#import <Masonry.h>
#import "XGConfig.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface OrderBillViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation OrderBillViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self ApptitileView:@"我的预购记录"];
    [self.view addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(KStatuBar_Height + 44);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"quxiaodingdanxuanhao" object:nil];
}

#pragma mark - UITableViewDelegate methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderBillTableViewCell *cell = [OrderBillTableViewCell cellWithTableview:tableView];
    NSDictionary *dic = self.dataSource[indexPath.row];
    NSString *redstr = dic[@"redStr"];
    NSString *bluestr = dic[@"blueStr"];
    
    NSString *touzhuStr = [NSString stringWithFormat:@"%@ %@",redstr,bluestr];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:touzhuStr];
    
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:[UIColor redColor]
                    range:NSMakeRange(0, redstr.length)];
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:[UIColor blueColor]
                    range:NSMakeRange(redstr.length + 1, bluestr.length)];
    cell.orderNumLbl.attributedText = attrStr;

    NSDate *touzhuDate = dic[@"touzhushijian"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设置格式：zzz表示时区
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //NSDate转NSString
    NSString *currentDateString = [dateFormatter stringFromDate:touzhuDate];
    cell.timeLbl.text = currentDateString;
    
    if ([self compareDate:dic[@"touzhushijian"]]) {
        cell.statusLbl.text = @"已开奖";
        cell.statusLbl.textColor = [UIColor grayColor];
    } else{
        cell.statusLbl.text = @"待开奖";
        cell.statusLbl.textColor = [UIColor greenColor];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = self.dataSource[indexPath.row];
    LotteryOrderView *view = [LotteryOrderView initView];
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.view addSubview:view];
    
    view.orderNumber = dic[@"dingdanhao"];
    NSString *redstr = dic[@"redStr"];
    NSString *bluestr = dic[@"blueStr"];
    
    NSString *touzhuStr = [NSString stringWithFormat:@"%@ %@",redstr,bluestr];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:touzhuStr];
    
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:[UIColor redColor]
                    range:NSMakeRange(0, redstr.length)];
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:[UIColor blueColor]
                    range:NSMakeRange(redstr.length + 1, bluestr.length)];
    view.attributedMyNumber = attrStr;
    view.openTime = dic[@"kaijiangshijian"];
    view.jiangchijine = dic[@"jiangchijine"];
    view.beishu = dic[@"beishu"];
    //    view.isTwoBtn = NO;
    view.isHiddenBottom = YES;
    view.zhudan = dic[@"zhudan"];
}

- (BOOL)compareDate:(NSDate *)aDate
{
    return aDate.timeIntervalSince1970 + 10*60 <= [NSDate date].timeIntervalSince1970;
}

#pragma mark - lazy methods
- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [UITableView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource{
    if (_dataSource == nil) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"MyOrderList.plist"];//这里就是你将要存储的沙盒路径（.plist文件，名字自定义）
        _dataSource = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];    }
    return _dataSource;
    
}

@end
